package fun.tan90.easy.binlog.worker.event;

import com.github.benmanes.caffeine.cache.LoadingCache;
import fun.tan90.easy.binlog.plugin.common.constants.DamiEventConstants;
import fun.tan90.easy.binlog.plugin.common.model.OutputParam;
import lombok.extern.slf4j.Slf4j;
import org.noear.dami.Dami;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * description TODO
 * company 铁人科技
 *
 * @author 陈浩杰
 * @date 2023/12/18 16:43
 */
@Slf4j
@Component
public class DamiBusEvent {

    @Resource
    LoadingCache<String, OutputParam> outputParamCache;

    @PostConstruct
    public void damiBusEvent() {
        Dami.<String, OutputParam>bus().listen(DamiEventConstants.OUTPUT_PARAM_TOPIC, payload -> {
            OutputParam outputParam = outputParamCache.get(payload.getContent());
            if (payload.isSubscribe() || payload.isRequest()) {
                payload.reply(outputParam);
            }
        });
    }
}
